package com.cmccgz.udacs_landlord_webapp.main.user.login;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.cmccgz.baselibrary.utils.RegularUtil;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.base.GlobalContext;
import com.cmccgz.udacs_landlord_webapp.bean.LoginInfo;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;

import java.util.Timer;
import java.util.TimerTask;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: INX
 * 登陆presenter
 */

public class LoginPresenter implements LoginContract.LoginPresenter {

    private final LoginContract.LoginView mLoginView;
    private Context mContext;
    private Timer timer;
    private int waitTime = 60;
    private boolean isWaiting=false;
    public final static int TYPE_PWD=0;
    public final static int TYPE_SMS=1;
    public LoginPresenter( LoginContract.LoginView view) {
        this.mContext = GlobalContext.getInstance();
        mLoginView = view;
    }

    @Override
    public void start() {

    }

    @Override
    public void doLogin(final String userNum, String password,int type) {
        if (!RegularUtil.isMobileNumber(userNum)) {
            mLoginView.loginError(mContext.getString(R.string.tips_phone_error));
            return;
        }

        if (type==TYPE_PWD&&!RegularUtil.isPassword(password)){
            mLoginView.loginError(mContext.getString(R.string.tips_pwd_error));
            return;
        }
        if (type==TYPE_SMS&&!RegularUtil.isSMSKey(password)) {
            mLoginView.loginError(mContext.getString(R.string.tips_sms_key_error));
            return;
        }
        ApiService.login(userNum, password,type+"")
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<ResultBean<LoginInfo>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mLoginView.loginError(mContext.getString(R.string.tips_login_fail));
                    }

                    @Override
                    public void onNext(ResultBean<LoginInfo> resultBean) {
                        if ("0".equals(resultBean.rsmsg.rscode)&&resultBean.resp!=null){
                            mLoginView.loginSuccess(resultBean.resp);
                        }else {
                            mLoginView.loginError(resultBean.rsmsg.rsdesc);
                        }
                    }
                });
    }

    @Override
    public void getUserHead(String userNum) {
        mLoginView.showUserHead("http://pic.qiantucdn.com/58pic/17/94/90/45w58PIC7j6_1024.jpg");
    }

    @Override
    public void getCode(String mobile,final Handler handler) {
        Context context = GlobalContext.getInstance();
        if (!RegularUtil.isMobileNumber(mobile)) {
            mLoginView.getCodeError(context.getString(R.string.tips_phone_error));
            return;
        }

        startWaiting(handler);

        //getcode
        ApiService.getSms(mobile,"0").subscribe(new Subscriber<ResultBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                stopWaiting();
                mLoginView.getCodeError("获取验证码失败！");
            }

            @Override
            public void onNext(ResultBean resultBean) {
                if ("0".equals(resultBean.rsmsg.rscode)) {
                    mLoginView.getCodeSuccess();
                }else{
                    stopWaiting();
                    mLoginView.getCodeError(resultBean.rsmsg.rsdesc);
                }
            }
        });
    }

    public void startWaiting(final Handler handler) {
        if (timer != null) {
            timer.cancel();
        }
        waitTime = 60;
        mLoginView.waitCode(waitTime );
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                waitTime--;
                if (handler != null) {
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.e("INX", "waitTime: isWaiting="+isWaiting+",waitTime="+waitTime );

                            if (!isWaiting) {
                                return;
                            }
                            mLoginView.waitCode(waitTime );
                        }
                    });
                }
                if (waitTime <= 0) {
                    timer.cancel();
                }
            }
        }, 1000, 1000);
        isWaiting=true;
    }

    public void stopWaiting() {
        if (timer!=null) {
            timer.cancel();
        }
        timer=null;
        mLoginView.resetCode();
        isWaiting=false;
    }

    @Override
    public void onDestroy() {
        stopWaiting();
    }
}
